// import { from } from 'core-js/fn/array'
import Vue from 'vue'
import VueRouter from 'vue-router'
// import Home from '../views/Home.vue'
import {checktoken} from '@/apis/user'
 import Login from '../views/Login.vue'



 
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Login',
    component: Login
  },
  {
    path: '/home',
    name: 'Home',
    //懒加载页面
    component: ()=> import('../views/Home.vue'),
    children:[{
      path:'/home/main',
      name:'home/main',
      component:()=>import('../views/homemodules/HomeMain.vue')
    },{
      //订单管理
      path:'/home/ordermanager',
      name:'home/ordermanager',
      component:()=>import('../views/homemodules/OrderManager.vue')
    },
    // 添加列表
    {
      path:'/home/accountList',
      name:'home/accountList',
      component:()=>import('../views/homemodules/AccountList.vue')
    },
    //添加账号
    {
      path:'/home/AddAccount',
      name:'home/AddAccount',
      component:()=>import('../views/homemodules/AddAccount.vue')
    },
    //修改密码
    {
      path:'/home/ChangePasw',
      name:'home/ChangePasw',
      component:()=>import('../views/homemodules/ChangePasw.vue')
    },
    //商品管理
    //商品添加
    {
      path:'/home/add',
      name:'home/add',
      component:()=>import('../views/homemodules/shopAdm/Add.vue')
    },
    {
      path:'/home/list',
      name:'home/list',
      component:()=>import('../views/homemodules/shopAdm/List.vue')
    },
    {
      path:'/home/cation',
      name:'home/cation',
      component:()=>import('../views/homemodules/shopAdm/Cation.vue')
    },
    //店铺管理
    {
      path:'/home/storeManagement',
      name:'home/storeManagement',
      component:()=>import('../views/homemodules/StoreManagement.vue')
    },
    //个人修改页面
    {
      path:'/home/Personal',
      name:'home/Personal',
      component:()=>import('../views/homemodules/Personal.vue')
    },
    {
      path:'/home/Personal',
      name:'home/Personal',
      component:()=>import('../views/homemodules/Personal.vue')
    },
    //销售统计
    {
      path:'/home/ShopStatistics',
      name:'home/ShopStatistics',
      component:()=>import('../views/homemodules/sales/ShopStatistics.vue')
    },
    {
      path:'/home/OrderStatistics',
      name:'home/OrderStatistics',
      component:()=>import('../views/homemodules/sales/OrderStatistics.vue')
    },]
  },
  {
    path:'*',
    component:()=> import('../views/Error404.vue')
  }
  // {
  //   path: '/about',
  //   name: 'About',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  // }
]

const router = new VueRouter({
  routes
})

//路由跳转前的生命周期
router.beforeEach((to,from,next)=>{
  //to：目标hash路由对象
  //from:  从哪个路由对象跳转
  // console.log(to,from,next);
      if(to.path != '/'){
        //访问除登录以外的其他页面
        checktoken({token:localStorage.token}).then(res=>{
          if(res.data.code==0){
            next()//如果在登录状态,直接放行允许访问对应页面
          }else{
              next('/')//返回登录界面,先登录才能访问核心内容
          }
        })

      }else{
        next()//放行,允许跳转到下一个hash

      }

})

export default router
